package org.sk.gsync.app;

import java.io.IOException;
import java.util.List;

import org.sk.gsync.app.mybatis.Folder;
import org.sk.gsync.app.spring.DatabaseSpringConfig;
import org.sk.gsync.app.spring.GeneralSpringConfig;
import org.sk.gsync.app.spring.GoogleDocsSpringConfig;
import org.sk.gsync.app.spring.GuiSpringConfig;
import org.sk.gsync.app.spring.MyBatisSpringConfig;
import org.sk.gsync.app.spring.SynchronizationSpringConfig;
import org.sk.gsync.app.synch.SynchronizationManager;
import org.sk.gsync.ui.LoginDialog;
import org.sk.gsync.ui.Principal;
import org.sk.gsync.ui.configuration.ConfigurationFrame;
import org.sk.gsync.ui.configuration.ConfigurationFrameFields;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.google.gdata.util.ServiceException;

public class SKSyncApplication {

    public static void main(final String[] args) {
        ApplicationContext ctx =
            new AnnotationConfigApplicationContext(
                GeneralSpringConfig.class, MyBatisSpringConfig.class,
                DatabaseSpringConfig.class, GoogleDocsSpringConfig.class,
                GuiSpringConfig.class, SynchronizationSpringConfig.class);

        // try {
        LoginDialog loginDialog = ctx.getBean(LoginDialog.class);
        loginDialog.login();
        ConfigurationFrame configFrame =
            ctx.getBean(ConfigurationFrame.class);
        configFrame.open();
        Principal principal = ctx.getBean(Principal.class);
        principal.setVisible(true);
        final SynchronizationManager syncManager =
            ctx.getBean(SynchronizationManager.class);
        final List<Folder> folder =
            (List<Folder>) configFrame.getContext().getValue(
                ConfigurationFrameFields.FOLDERS_TO_SYNC);
        try {
            syncManager.mapGDocs();
            syncManager.mapLocal(folder.get(0));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    // try {
                    // syncManager.mapGDocs();
                    // syncManager.mapLocal(folder.get(0));
                    // Thread.sleep(3000);
                    // } catch (ServiceException e) {
                    // e.printStackTrace();
                    // } catch (IOException e) {
                    // e.printStackTrace();
                    // } catch (InterruptedException e) {
                    // e.printStackTrace();
                    // }
                }
            }
        };
        t.start();
        syncManager.startLocalWatcher(folder.get(0));
        // SynchDispatcher dispatcher = ctx.getBean(SynchDispatcher.class);
        // dispatcher.start();
        // GoogleDocsMapping mapping =
        // ctx.getBean(GoogleDocsMapping.class);
        // mapping.map();
        // catch (ServiceException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
    }
}
